package com.example.demoplus.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.demoplus.R;
import com.example.demoplus.api.Datas;
import java.util.List;

public class QqAdapter extends RecyclerView.Adapter<QqAdapter.QqViewHolder>{
    private List<Datas> datas;
    private Context context;

    public QqAdapter(Context context) {
        this.context = context;
    }

    //    获取显示的布局
    @NonNull
    @Override
    public QqAdapter.QqViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        获取Item布局文件，并且转化为View对象
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_qq_app, parent, false);
        return new QqAdapter.QqViewHolder(view);
    }
    //    绑定数据
    @Override
    public void onBindViewHolder(@NonNull QqAdapter.QqViewHolder holder, int position) {
//        获取当前数据
        Datas data = this.datas.get(position);
//        找到排名并设置显示内容
        holder.tvIndex.setText(String.valueOf(position+1));//序号
        holder.tvName.setText(data.getList().get(0).getTitle());
        holder.tvSinger.setText(data.getList().get(0).getSinger_name()); //获取到第一个歌手的信息
        Glide.with(context).load(data.getList().get(0).getPic()).into(holder.img);
    }
    //    获取数据总量
    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }
    //    设置数据
    public void setData(List<Datas> datas) {
        this.datas = datas;
        //刷新数据
        notifyDataSetChanged();
    }

    //    创建一个内部类作为泛型
    static class QqViewHolder extends RecyclerView.ViewHolder{
        //        排名
        TextView tvIndex;
        //        App名字
        TextView tvName;
        TextView tvSinger;
        ImageView img;

        public QqViewHolder(@NonNull View itemView) {
            super(itemView);
            tvIndex = itemView.findViewById(R.id.tv_index);
            tvName = itemView.findViewById(R.id.tv_name);
            tvSinger = itemView.findViewById(R.id.tv_singer);
            img = itemView.findViewById(R.id.img);
        }
    }
}
